import SatRow from '@/components/table/sat-row'
import SatRowCode from '@/components/table/sat-row-code'
import SatRowAlternative from '@/components/table/sat-row-alternative'

export default {
    data() {
        return {
            tableLoading: false,
            tableData: [],
            currentOrderId: '',
            currentTaskId: '',
            remarkDialogShow: false,
            currentOuterCode: '',
            page: {
                currentPage: 1,
                pageSize: 10,
                orderBy: null,
                total: 0,
            },
            lastData: null,
			// pageChangeVisibility 浏览器标签切换时刷新数据
			pageChangeVisibility: false,
			visibilitychange: null,
            // tableHeight: 'calc(100% - 230}px)',


            markTableLoading: false,
            markTableData:[],
            markPage: {
                currentPage: 1,
                pageSize: 10,
                orderBy: '',
                total: 0,
            },
        }
    },
    components: {SatRow, SatRowCode,SatRowAlternative},
	deactivated() {
    	if(this.pageChangeVisibility){
			console.log("deactivated" + this._uid)
			document.removeEventListener('visibilitychange', this.visibilitychange);
		}
	},
    activated() {
		if(this.pageChangeVisibility){
			console.log("actived" +  this._uid)
			document.addEventListener('visibilitychange', this.visibilitychange);
		}
    },
    mounted() {
        this.visibilitychange = async () => {
			if (!document.hidden) {
				await this.fetchList()
			}
		}
    },
    // updated(){
    // 	this.$nextTick(()=>{
    // 		this.tableHeight = `calc(100% - ${this.$refs.searchForm ? this.$refs.searchForm.$el.clientHeight: 230}px)`
    // 		console.log('tableHeight', this.tableHeight)
    // 	})
    // },
    computed: {},
    watch: {
        // async $route () {
        // 	if(this.$route)
        // },
    },
    methods: {
        async tableFetch(url, param, getFlag) {
            try {
                console.log(param)
                this.compareData(JSON.parse(param))
                this.tableLoading = true
                if (getFlag) {
                    let {list, total} = await this.$get(url, param)
                    this.tableData = list || []
                    this.page.total = total || 0
                } else {
                    let {list, total} = await this.$post(url, param)
                    this.tableData = list || []
                    this.page.total = total || 0
                }
            } catch (e) {
                this.tableData = []
                this.page.total = 0
            } finally {
                this.tableLoading = false
            }
        },

        compareData(data) {
            let updated = false
            if (this.lastData !== null) {
                for (let key in data) {
                    if (!'currentPage,pageSize,total'.includes(key) && data[key] !== this.lastData[key]) {
                        updated = true
                        break
                    }
                }
                if (updated) {
                    this.page.total = 0
                    this.page.currentPage = 1
                    data.currentPage = 1
                    data.currentPage = 1
                }
            }
            this.lastData = data
        },
        async _restForm(form = 'form') {
            try {
                if (this.$refs[form]) {
                    this.$refs[form].resetFields()
                    this.$refs[form].clearValidate()
                }
            } finally {
                this.tableLoading = false
                this.tableData = []
                this.page.total = 0
                this.page.currentPage = 1
            }

        },
    },
}
